from django.forms import model_to_dict
from django.http import JsonResponse
from django.views import View

from navigation.models import FirstMenu
from utils.common import res_data


class Menu(View):

    def get(self, request, *args, **kwargs):
        # 先判断用户是否登录
        if not request.user.is_authenticated:
            return JsonResponse(res_data(ret=-1, msg="user unlogin"))
        first_menus = FirstMenu.objects.all()
        menus = []
        for item in first_menus:
            send_menus = item.secondmenu_set.all().values()
            menu = model_to_dict(item)
            menu["children"] = list(send_menus)
            menus.append(menu)
        res = res_data(1, "success", menus)
        return JsonResponse(res)
